package com.hz.ws;

import com.hz.base.beans.vo.CommonResult;
import com.hz.base.beans.vo.Page;
import com.hz.base.beans.vo.PageEntity;
import com.hz.base.beans.vo.QueryParams;
import com.hz.beans.user.entity.AlipayOrderEntity;
import com.hz.beans.user.entity.TCounselling;
import com.hz.beans.user.entity.TReply;
import com.hz.interfaces.user.inf.ITCounsellingInterface;
import com.hz.interfaces.user.inf.ITReplyInterface;
import com.hz.sys.utils.WebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author mcz
 * @Date 2018/9/21 10:58
 * @Description 咨询控制器
 * @Version 1.0
 */
@Api(tags = "咨询管理",description = "CURD")
@Controller
@RequestMapping("/Counselling")
public class CounsellingController {

    @Autowired
    private ITCounsellingInterface itCounsellingInterface;
    @Autowired
    private ITReplyInterface itReplyInterface;

    //返回添加咨询页面
    @RequestMapping(value = "/getCounsView" ,method = {RequestMethod.POST,RequestMethod.GET})
    public String getCounsView(){
        return "page/releaseRequirements";
    }

    @ApiOperation(value="获取咨询", notes="根据ID查询")
    @ApiImplicitParam(name = "id", value = "咨询ID", required = true, dataType = "Long", paramType = "query")
    @RequestMapping(value = "/queryCounselling" ,method = {RequestMethod.GET})
    @ResponseBody
    public CommonResult<TCounselling> getCounsellingById(@RequestParam Long id){
        /*try {
            Thread.sleep(15000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        return itCounsellingInterface.queryTCounselling(id);
    }

    @ApiOperation(value="获取咨询列表", notes="根据ID查询")
    @RequestMapping(value = "/queryCounsellingPage" ,method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public CommonResult<PageEntity> getCounsellingPage(@RequestBody QueryParams<TCounselling> queryParams){
        return itCounsellingInterface.queryForPage(queryParams);
    }

    //获取需求list数据集合页面
    @RequestMapping(value = "/queryCounseModelAndView" ,method = {RequestMethod.POST,RequestMethod.GET})
    public ModelAndView queryCounseModelAndView(TCounselling tCounselling, Page page,String isRecetip){
        ModelAndView modelAndView=new ModelAndView();
        if(StringUtils.isNotBlank(isRecetip)){
            tCounselling.setIsReceipt(isRecetip);
        }
        CommonResult<PageEntity> results=queryForPage(tCounselling,page);
        modelAndView.addObject("results",results);
        modelAndView.setViewName("page/requirelList");
        return modelAndView;
    }
    //刷新，查询局部刷新
    @RequestMapping(value = "/refreshAjax",method = {RequestMethod.GET,RequestMethod.POST})
    public String refreshAjax(TCounselling tCounselling, Page page, String  refreshName,Map<String,Object> map){
        CommonResult<PageEntity> results=queryForPage(tCounselling,page);
        map.put("results",results);
        return "page/requirelList#"+refreshName;
    }

    //根据ID获取需求实体列数据集合页面
    @RequestMapping(value = "/queryCounseEntityModelAndView/{id}" ,method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public ModelAndView queryCounseEntityModelAndView(@PathVariable(name="id") Long consuid){
        ModelAndView modelAndView=new ModelAndView();
        CommonResult<TCounselling> results=itCounsellingInterface.queryTCounselling(consuid);
        List<TReply> replyResult=itReplyInterface.queryTReplyAndQuesList(results.getData());
        modelAndView.addObject("reply",replyResult);
        modelAndView.addObject("results",results);
        modelAndView.setViewName("page/requireDetail");
        return modelAndView;
    }


    @ApiOperation(value="添加咨询", notes="添加咨询参数")
    @RequestMapping(value = "/addCounselling" ,method = {RequestMethod.POST})
    @ResponseBody
    public CommonResult<TCounselling> addCounselling(TCounselling tCounselling){
        CommonResult<TCounselling> result=itCounsellingInterface.addTCounselling(tCounselling);
        String basePath=WebUtil.getBasePath();
        String notiUrl=basePath+"Counselling/aliPayReturn";
        //设置回调地址
        result.getData().setIsReceipt(notiUrl);
        return result;
    }
    /**
     * @Author mcz
     * @Description  分页数据查询
     * @Date 2018/10/15 11:17
     * @Param  TRecipient tRecipient, Page page
     * @return  CommonResult<PageEntity>
     **/
    private  CommonResult<PageEntity> queryForPage(TCounselling tCounselling, Page page){
        QueryParams<TCounselling> queryParams=new QueryParams<>();
        queryParams.setEntity(tCounselling);
        queryParams.setPaging(page);
        CommonResult<PageEntity> results=itCounsellingInterface.queryForPage(queryParams);
        return  results;
    }

    //al支付异步回调函数   异步回到确保不漏单
    @RequestMapping(value = "/aliPayReturn" ,method = {RequestMethod.POST,RequestMethod.GET})
    public void aliPayReturn(HttpServletRequest request, HttpServletResponse response){
        if(WebUtil.isSign(request)){
            List<TCounselling> list=null;
            //获取订单状态
            String tradeStatus = request.getParameter("trade_status");
            //支付成功
            if("trade_success".equalsIgnoreCase(tradeStatus)){
                //获取我们传过去的订单号
                String orderNo = request.getParameter("out_trade_no");
                //获取收款金额
                String totalAmount = request.getParameter("receipt_amount");
                TCounselling tCounselling=new TCounselling();
                tCounselling.setOrderNo(orderNo);
                BigDecimal bigDecimal=new BigDecimal(totalAmount);
                tCounselling.setPayMoney(bigDecimal);
                 list=itCounsellingInterface.queryPayStatus(tCounselling);
            }
            WebUtil.writeJson(response,"success");
        }else{
            WebUtil.writeJson(response,"fail");
        }
    }








    /**
     * @Author mcz
     * @Description  跳转二维码支付页面
     * @Date 2018/11/2 16:51
     * @Param 
     * @return 
     **/
    @RequestMapping(value = "/getAliQRPayPage" ,method = {RequestMethod.GET,RequestMethod.POST})
    public String getAliQRPayPage(String type, AlipayOrderEntity alipayOrderEntity, Model model){
        String basePath=WebUtil.getBasePath();
        String notiUrl=basePath+"Counselling/aliPayReturn";
        String retUrl=basePath+"Counselling/queryCounseModelAndView";
        alipayOrderEntity.setNotifyUrl(notiUrl);
        alipayOrderEntity.setReturnUrl(retUrl);
        model.addAttribute("alipayOrderEntity",alipayOrderEntity);
        return "page/payPage/aliPayQRPage";
    }


}
